package com.rchat.pocmini.activity.invitefriendtogroup;

import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.aso.common.http.bean.BaseBean;
import com.aso.common.http.delegate.HttpCallbackImpl;
import com.rchat.pocmini.bean.SearchUserBean;
import com.rchat.pocmini.bean.UserNoInGroupBean;
import com.rchat.pocmini.bean2eventbus.SelectedFriendInviteToGroupBean;
import com.rchat.pocmini.global.HttpCodeGlobal;
import com.rchat.pocmini.global.UrlsGlobal;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by ljd on 2017/9/1.
 */

public class InviteFriendToGroupPresenterImpl extends InviteFriendToGroupContract.Presenter {

    private ArrayList<UserNoInGroupBean.DataBean.UsersBean.UserBean> data;
    private ArrayList<UserNoInGroupBean.DataBean.UsersBean.UserBean> selectedData;


    @Override
    public void onPStart() {
        super.onPStart();
        EventBus.getDefault().register(this);
        data = new ArrayList<>();
        selectedData = new ArrayList<>();
    }


    @Override
    void queryFriendNotInGroup(String userName, String groupId) {

        mModel.queryFriendNotInGroup(groupId, userName, new HttpCallbackImpl<UserNoInGroupBean>() {

            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(UserNoInGroupBean userNoInGroupBean) {
                if (mView == null) {
                    return;
                }
                data.addAll(userNoInGroupBean.getData().getUsers().getUser());
                mView.queryFriendNotInGroup(data);
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(bean.getDesc());
            }

            @Override
            public void onFinish() {

            }
        });

    }


    @Override
    void inviteFriendToGroup(String groupId) {
        mModel.inviteFriendToGroup(groupId, selectedData, new HttpCallbackImpl<SearchUserBean>() {
            @Override
            public void onStart() {
                clearSelected();
            }

            @Override
            public void onSuccess(SearchUserBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(bean.getDesc());
            }

            @Override
            public void onFailureResult(BaseBean bean) {
                if (mView == null) {
                    return;
                }
                mView.showShortToast(bean.getDesc());
            }

            @Override
            public void onFinish() {

            }
        });


    }

    @Override
    void clearSelected() {
        for (UserNoInGroupBean.DataBean.UsersBean.UserBean u : data) {
            u.setSelected(false);
        }
        mView.queryFriendNotInGroup(data);
        selectedData.clear();
        mView.showSelectedCount(View.GONE, null);
        mView.showSelectedListView(View.GONE, null);
    }

    @Override
    void searchFriend(String account) {
        hideInputMethodManager();
        ArrayList<UserNoInGroupBean.DataBean.UsersBean.UserBean> searchData = new ArrayList<>();

        if (account.equals("")) {
            searchData.addAll(data);
            mView.queryFriendNotInGroup(searchData);
            return;
        }


        for (UserNoInGroupBean.DataBean.UsersBean.UserBean bean : data) {
            if (bean.getUserName().contains(account) || bean.getDisplay().contains(account)) {
                searchData.add(bean);
            }
        }
        mView.queryFriendNotInGroup(searchData);

    }


    @Override
    public void onPDestroy() {
        EventBus.getDefault().unregister(this);
        super.onPDestroy();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBusInviteFriend(SelectedFriendInviteToGroupBean b) {
        boolean show = false;
        if (b.isCheck()) {
            for (UserNoInGroupBean.DataBean.UsersBean.UserBean bean : data) {
                if (bean.getUserName().equals(b.getUserName())) {
                    show = true;
                    selectedData.add(bean);
                }
            }
            if (show) {
                String totalAllMembers = "已选%1$d位成员";
                mView.showSelectedCount(View.VISIBLE, String.format(totalAllMembers, selectedData.size()));
                mView.showSelectedListView(View.VISIBLE, selectedData);
            }
        } else {
            for (UserNoInGroupBean.DataBean.UsersBean.UserBean bean : data) {
                if (bean.getUserName().equals(b.getUserName())) {
                    selectedData.remove(bean);
                }
            }
            if (selectedData == null || selectedData.isEmpty() || selectedData.size() == 0) {
                mView.showSelectedListView(View.GONE, null);
                mView.showSelectedCount(View.GONE, null);
            } else {
                mView.showSelectedListView(View.VISIBLE, selectedData);
                String totalAllMembers = "已选%1$d位成员";
                mView.showSelectedCount(View.VISIBLE, String.format(totalAllMembers, selectedData.size()));
            }
        }
    }

    private void hideInputMethodManager() {
        ((InputMethodManager) mActivity
                .getSystemService(Context.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(mActivity
                                .getCurrentFocus().getWindowToken(),
                        InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
